Introduction
Xamarin.Uitest is a testing framework using which we can automate our application in C# using the Nunit framework which will run on Android and iOS.
Each Uitest is written as methods and is referred to as tests.
The test cases will interact with the UI and perform the operations just as a user does like tapping on buttons, entering text and performing gestures like swiping or pinching.
Setting up Xamarin UiTest
File-->New Solution-->Multiplatform-->Tests-->Solution name.
After creating the project the solution will look like this:
We will start with App Initializer and will try to understand how it works.
- public static IApp StartApp(Platform platform)
- {
-
-
-
-
-
-
-
-
-
-
-
- if (platform == Platform.Android)
- {
- return ConfigureApp
- .Android
-
-
-
- .StartApp();
- }
- return ConfigureApp
- .iOS
-
-
-
- .StartApp();
- }
Start App will be the first function called when starting the Test Execution.
So here we have to do the Setup for iOS and Android.
Android Setup
We can either use the application that is already there on the Device using the Package name of the Application or we can install the apk from a specified location.
-
- return ConfigureApp
- .Android
- .Debug()
- .InstalledApp(“com.Abc.XYZ”)
- .DeviceSerial(“ce081718341fbf1f01")
- .StartApp();
-
-
- return ConfigureApp
- .Android
- .Debug()
- .ApkFile(“../../../com.Abc.XYZ”)
- .DeviceSerial(“ce081718341fbf1f05")
- .StartApp();
Getting the Serial Number of Device,
- adb devices
- List of devices attached
- ce081718341fbf1f05 device
- emulator-5554 device
iOS Setup
-
- return ConfigureApp
- .iOS
- .Debug()
- .InstalledApp("com.Abc.XYZ")
- .StartApp();
-
-
- return ConfigureApp
- .iOS
- .Debug()
- .AppBundle("../../../iOSAppProject/bin/iPhoneSimulator/Debug/iosapp.app")
- .StartApp();
Addition Setup Required for iOS
In your Ios Application, install Xamarin.TestClound.Agent .
In the finished launching function of iOS put the below code if you want to be enabled always .
- Xamarin.Calabash.Start();
Even you can put condition on enabling it .
#if ENABLE_TEST_CLOUD
Xamarin.Calabash.Start();
#endif
Make sure test cloud is enabled for iOS apps .
Steps to enable,
Right click on solution file-->options -->Complier--> define symbols
Add - ENABLE_TEST_CLOUD;
Now our Android and iOS setup are finished. Let's try to understand how we will automate that .
We are taking a scenario of Login Validation.
- IApp App;
- Query _Username = x => x.Marked("username");
- Query _Password = x => x.Marked("username");
- App.Tap(_Username);
- App.EnterText("Valid Username");
- App.Tap(_Password);
- App.EnterText("Valid Password");
- App.Tap("Login");
Now we have successfully executed the login request.
To crosscheck whether we have successfully logged in or not we need to wait until it navigates to the next page.
So let's see how we can achieve that.
- App.WaitForElement(l => l.Marked("Login Success"), timeout: TimeSpan.FromSeconds(100));
- Assert.Pass(" we have succefully Logged In ");
So with this article we have suceesfully done the setup for UI Automation and we have gone through a scenario on how to execute it by taking the login example.
I am still working on some complex scenarios and will publish that soon!!!!
Happy Coding :)